package com.example.random_clap.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.random_clap.common.R;
import com.example.random_clap.common.constant.MessageConstant;
import com.example.random_clap.entity.Evaluation;
import com.example.random_clap.entity.Problem;
import com.example.random_clap.service.EvaluateService;
import com.example.random_clap.service.ProblemService;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 评价模块
 *
 * @author ouyangkai
 * @date 2022-10-30
 */
@Slf4j
@RestController
@RequestMapping("/evaluation")
public class EvaluateController {

    @Autowired
    private EvaluateService evaluationService;

    @Autowired
    private ProblemService problemService;


    /**TODO 未完成
     * 根据评价状态获取评价问题
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getEvaluation")
    public R<Page<Problem>> getEvaluation(int currentPage, int pageSize, Long userId,int status) {
        Page<Problem> pageInfo = problemService.getNoEvaluateProblem(currentPage,pageSize,userId,status);

        return R.success(pageInfo);
    }

    /**
     * 评价问题
     * @param evaluation
     * @return
     */
    @PostMapping
    public R<String> evaluate(int status,@RequestBody Evaluation evaluation){

        //获取问题id
        Long problemId = evaluation.getProblemId();
        Problem problem = problemService.getById(problemId);
        problem.setStatus(status);
        //根据id修改问题状态(已评价)
        problemService.updateById(problem);

        //新增评价信息
        boolean res = evaluationService.save(evaluation);
        if(!res){
            return R.success(MessageConstant.EVALUATE_PROBLEM_ERROR);
        }

        return R.success(MessageConstant.EVALUATE_PROBLEM_SUCCESS);
    }




}
